<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class Product extends Model implements Sortable
{
    use HasFactory,SortableTrait,HasDateTimeFormatter;

    protected $sortable = [
        // 设置排序字段名称
        'order_column_name' => 'sort',
        // 是否在创建时自动排序，此参数建议设置为true
        'sort_when_creating' => true,
    ];

    public function category()
    {
        return $this->belongsToMany(Category::class,'product_categories','product_id','category_id');
    }

    public function labels()
    {
        return $this->belongsToMany(Label::class,'product_labels');
    }

    public function attribute(){
        return $this->hasMany(ProductAttribute::class);
    }

    public function skus(){
        return $this->hasMany(ProductSku::class);
    }
}
